﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using Warehouse.API.Applcation.Command.AllocateCommand;
using Warehouse.Domain;
using Warehouse.Infrstructre;
using Warehouse.Infrstructre.Interface;

namespace Warehouse.API.Applcation.CommandHandlers.AllocateCommandHandlers
{
    public class AllocateUpdCommandHandlers : IRequestHandler<AllocateUpdCommand, int>
    {
        private readonly IAllocateRepository _allocateRepository;
        protected readonly EFCoreDBcontext _dbContext;
        protected readonly IMapper mapper;
        private readonly IAllProductRepsitory _allProductRepsitory;
        public AllocateUpdCommandHandlers(IAllProductRepsitory _allProductRepsitory, IAllocateRepository _allocateRepository, EFCoreDBcontext _dbContext, IMapper mapper)
        {
            this._allProductRepsitory = _allProductRepsitory;
            this._allocateRepository = _allocateRepository;
            this.mapper = mapper;
            this._dbContext = _dbContext;
        }
        public async Task<int> Handle(AllocateUpdCommand request, CancellationToken cancellationToken)
        {
            using (var tran = await _dbContext.Database.BeginTransactionAsync((System.Data.IsolationLevel.Serializable)))
            {
                try
                {

                    var se = (await _allocateRepository.GetModelAsync(request.addDto.AllocateId));
                    se.AllocateCode = request.addDto.AllocateCode;
                    se.AllocateStore = request.addDto.AllocateStore;
                    se.MorderNumber = request.addDto.MorderNumber;
                    se.MakePerson = request.addDto.MakePerson;
                    se.MarkTime = request.addDto.MarkTime;
                    se.ARremark = request.addDto.ARremark;
                    se.AllocateState = request.addDto.AllocateState;
                    se.IsAllocate = request.addDto.IsAllocate;
                    se.office_id = request.addDto.office_id;
                    se.user_id = request.addDto.user_id;
                    se.ReasonReview = request.addDto.ReasonReview;
                    se.UpdTime = DateTime.Now;
                    se.UpdName = request.addDto.UpdName;

                    // 先处理 procurement1
                    var taskToAdd = await _allocateRepository.UpdateAsync(se);


                    //查询调拨产品信息
                    var infolist = (await _allProductRepsitory.GetAllAsyns()).Where(x => x.AllocateId == request.addDto.AllocateId);
                    //先删除原有入库产品信息
                    foreach (var item in infolist)
                    {
                        await _allProductRepsitory.RemoveAsync(item);
                    }



                    foreach (var item in request.addDto.AllProducs)
                    {
                        AllProduct detail = new AllProduct
                        {
                            ProductsID = item.ProductsID,
                            AllocateId = se.AllocateId,
                            AllProductNum = item.AllProductNum,
                            StorehouseId = item.StorehouseId,
                            office_id = item.office_id,
                            user_id = item.user_id,

                        };
                        await _allProductRepsitory.AddAsync(detail);
                    }

                    // 提交事务
                    await tran.CommitAsync();

                    return 1;  // 返回最终的结果
                }
                catch (Exception)
                {
                    // 回滚事务  
                    await tran.RollbackAsync();
                    throw;
                }
            }



        }
    }
}
